package com.huawei.hms.framework.network.grs.local;

import android.text.TextUtils;
import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.network.grs.local.model.b;
import com.huawei.hms.framework.network.grs.local.model.d;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.json.JSONArray;
import org.json.JSONObject;

public class c extends a {

    public static class a {
        static final c a = new c();
    }

    private c() {
        if (d("grs_app_global_route_config.json") == 0) {
            this.d = true;
        }
    }

    public static c a() {
        return a.a;
    }

    private void a(JSONArray jSONArray) {
        if (jSONArray != null && jSONArray.length() != 0) {
            for (int i = 0; i < jSONArray.length(); i++) {
                JSONObject jSONObject = jSONArray.getJSONObject(i);
                com.huawei.hms.framework.network.grs.local.model.c cVar = new com.huawei.hms.framework.network.grs.local.model.c();
                String string = jSONObject.getString("name");
                cVar.a(string);
                if (!TextUtils.isEmpty(string) && TextUtils.isEmpty((CharSequence) this.e.get(string))) {
                    List a;
                    this.e.put(string, jSONObject.toString());
                    cVar.c(jSONObject.getString("routeBy"));
                    JSONArray jSONArray2 = jSONObject.getJSONArray("servings");
                    for (int i2 = 0; i2 < jSONArray2.length(); i2++) {
                        JSONObject jSONObject2 = (JSONObject) jSONArray2.get(i2);
                        d dVar = new d();
                        String str = "no-country";
                        if (jSONObject2.has("countryOrAreaGroup")) {
                            str = jSONObject2.getString("countryOrAreaGroup");
                        } else if (jSONObject2.has("countryGroup")) {
                            str = jSONObject2.getString("countryGroup");
                        } else {
                            Logger.i("LocalManagerV2", "maybe this service routeBy is unconditional.");
                        }
                        dVar.a(str);
                        JSONObject jSONObject3 = jSONObject2.getJSONObject("addresses");
                        Map hashMap = new HashMap(16);
                        Iterator keys = jSONObject3.keys();
                        while (keys.hasNext()) {
                            String str2 = (String) keys.next();
                            hashMap.put(str2, jSONObject3.getString(str2));
                        }
                        dVar.a(hashMap);
                        cVar.a(dVar.a(), dVar);
                    }
                    if (jSONObject.has("countryOrAreaGroups")) {
                        a = a(jSONObject.getJSONArray("countryOrAreaGroups"), null);
                    } else if (jSONObject.has("countryGroups")) {
                        a = a(jSONObject.getJSONArray("countryGroups"), null);
                    } else {
                        Logger.i("LocalManagerV2", "service use default countryOrAreaGroup");
                        a = null;
                    }
                    cVar.a(a);
                    if (this.a == null) {
                        this.a = new com.huawei.hms.framework.network.grs.local.model.a();
                    }
                    this.a.a(string, cVar);
                }
            }
        }
    }

    public int a(String str) {
        this.a = new com.huawei.hms.framework.network.grs.local.model.a();
        try {
            JSONObject jSONObject = new JSONObject(str).getJSONArray("applications").getJSONObject(0);
            this.a.a(jSONObject.getString("name"));
            JSONArray jSONArray = jSONObject.getJSONArray("services");
            List arrayList = new ArrayList(16);
            if (jSONArray == null || jSONArray.length() == 0) {
                Logger.w("LocalManagerV2", "local config application.services is not config or no any services");
                return -1;
            }
            for (int i = 0; i < jSONArray.length(); i++) {
                arrayList.add((String) jSONArray.get(i));
            }
            if (jSONObject.has("customservices")) {
                a(jSONObject.getJSONArray("customservices"));
            }
            return 0;
        } catch (Throwable e) {
            Logger.w("LocalManagerV2", "parse appbean failed maybe json style is wrong.", e);
            return -1;
        }
    }

    public List<b> a(JSONArray jSONArray, JSONObject jSONObject) {
        if (jSONArray == null || jSONArray.length() == 0) {
            return new ArrayList();
        }
        try {
            List<b> arrayList = new ArrayList(16);
            for (int i = 0; i < jSONArray.length(); i++) {
                JSONArray jSONArray2;
                JSONObject jSONObject2 = jSONArray.getJSONObject(i);
                b bVar = new b();
                bVar.a(jSONObject2.getString("id"));
                bVar.b(jSONObject2.getString("name"));
                bVar.c(jSONObject2.getString("description"));
                if (jSONObject2.has("countriesOrAreas")) {
                    jSONArray2 = jSONObject2.getJSONArray("countriesOrAreas");
                } else if (jSONObject2.has("countries")) {
                    jSONArray2 = jSONObject2.getJSONArray("countries");
                } else {
                    Logger.w("LocalManagerV2", "current country or area group has not config countries or areas.");
                    jSONArray2 = null;
                }
                Set hashSet = new HashSet(16);
                if (jSONArray2 == null || jSONArray2.length() == 0) {
                    Logger.w("LocalManagerV2", "local config countryGroups.countries is not config or no any countries");
                    return new ArrayList();
                }
                for (int i2 = 0; i2 < jSONArray2.length(); i2++) {
                    hashSet.add((String) jSONArray2.get(i2));
                }
                bVar.a(hashSet);
                arrayList.add(bVar);
            }
            return arrayList;
        } catch (Throwable e) {
            Logger.w("LocalManagerV2", "parse countrygroup failed maybe json style is wrong.", e);
            return new ArrayList();
        }
    }

    public int b(String str) {
        try {
            a(new JSONObject(str).getJSONArray("services"));
            return 0;
        } catch (Throwable e) {
            Logger.w("LocalManagerV2", "parse 2.0 services failed maybe because of json style.please check!", e);
            return -1;
        }
    }

    public int c(String str) {
        this.b = new ArrayList(16);
        try {
            JSONArray jSONArray;
            JSONObject jSONObject = new JSONObject(str);
            if (jSONObject.has("countryOrAreaGroups")) {
                jSONArray = jSONObject.getJSONArray("countryOrAreaGroups");
            } else if (jSONObject.has("countryGroups")) {
                jSONArray = jSONObject.getJSONArray("countryGroups");
            } else {
                Logger.e("LocalManagerV2", "maybe local config json is wrong because the default countryOrAreaGroups isn't config.");
                jSONArray = null;
            }
            if (jSONArray == null) {
                return -1;
            }
            if (jSONArray.length() != 0) {
                for (int i = 0; i < jSONArray.length(); i++) {
                    JSONArray jSONArray2;
                    jSONObject = jSONArray.getJSONObject(i);
                    b bVar = new b();
                    bVar.a(jSONObject.getString("id"));
                    bVar.b(jSONObject.getString("name"));
                    bVar.c(jSONObject.getString("description"));
                    if (jSONObject.has("countriesOrAreas")) {
                        jSONArray2 = jSONObject.getJSONArray("countriesOrAreas");
                    } else if (jSONObject.has("countries")) {
                        jSONArray2 = jSONObject.getJSONArray("countries");
                    } else {
                        Logger.w("LocalManagerV2", "current country or area group has not config countries or areas.");
                        jSONArray2 = null;
                    }
                    Set hashSet = new HashSet(16);
                    if (jSONArray2 == null || jSONArray2.length() == 0) {
                        Logger.w("LocalManagerV2", "local config countryGroups.countries is not config or no any countries");
                        return -1;
                    }
                    for (int i2 = 0; i2 < jSONArray2.length(); i2++) {
                        hashSet.add((String) jSONArray2.get(i2));
                    }
                    bVar.a(hashSet);
                    this.b.add(bVar);
                }
            }
            return 0;
        } catch (Throwable e) {
            Logger.w("LocalManagerV2", "parse countrygroup failed maybe json style is wrong.", e);
            return -1;
        }
    }
}
